home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Entertainment / tblt / tblt⁄grid.c < prev    next >
Text File  |  1986-09-06  |  6KB  |  175 lines

  1. /*
  2.  * grid.c - support for the grid abstraction of the board
  3.  */
  4.  
  5. #include <quickdraw.h>
  6. #include <window.h>
  7. #include <memory.h>
  8. #include "tablut.h"
  9.  
  10. /*
  11.  * setpieces() - put all the board pieces in their initial places
  12.  */
  13. setpieces()
  14. {
  15.     int i;
  16.  
  17.     for (i = 0; i < NUMPIECES; ++i) {
  18.     hidepiece(i);
  19.     }
  20.  
  21.     placepiece(FIRSTSWEDE  ,  0, -2); placepiece(FIRSTSWEDE+1,  0, -1);
  22.     placepiece(FIRSTSWEDE+2, -2,  0); placepiece(FIRSTSWEDE+3, -1,  0);
  23.     placepiece(FIRSTSWEDE+4,  1,  0); placepiece(FIRSTSWEDE+5,  2,  0);
  24.     placepiece(FIRSTSWEDE+6,  0,  1); placepiece(FIRSTSWEDE+7,  0,  2);
  25.  
  26.     placepiece(FIRSTMUSC   , -1, -4); placepiece(FIRSTMUSC+ 1,  0, -4);
  27.       placepiece(FIRSTMUSC+ 2,  1, -4); placepiece(FIRSTMUSC+ 3,  0, -3);
  28.     placepiece(FIRSTMUSC+ 4,  4, -1); placepiece(FIRSTMUSC+ 5,  4,  0);
  29.       placepiece(FIRSTMUSC+ 6,  4,  1); placepiece(FIRSTMUSC+ 7,  3,  0);
  30.     placepiece(FIRSTMUSC+ 8,  1,  4); placepiece(FIRSTMUSC+ 9,  0,  4);
  31.       placepiece(FIRSTMUSC+10, -1,  4); placepiece(FIRSTMUSC+11,  0,  3);
  32.     placepiece(FIRSTMUSC+12, -4,  1); placepiece(FIRSTMUSC+13, -4,  0);
  33.       placepiece(FIRSTMUSC+14, -4, -1); placepiece(FIRSTMUSC+15, -3,  0);
  34.  
  35.     placepiece(THEKING, 0, 0);
  36. }
  37.  
  38. /*
  39.  * makehomes() - initialize the off-board home for each piece.
  40.  *  This code assumes that *home[].tenant is initialized to 0.
  41.  */
  42. makehomes()
  43. {
  44.     SetPt(&(kinghome[0].hpoint), -6, 0); gridtopoint(&(kinghome[0].hpoint));
  45.  
  46.     SetPt(&(swedhome[0].hpoint), -6, 1); gridtopoint(&(swedhome[0].hpoint));
  47.     SetPt(&(swedhome[1].hpoint), -6, 2); gridtopoint(&(swedhome[1].hpoint));
  48.     SetPt(&(swedhome[2].hpoint), -6, 3); gridtopoint(&(swedhome[2].hpoint));
  49.     SetPt(&(swedhome[3].hpoint), -6, 4); gridtopoint(&(swedhome[3].hpoint));
  50.     SetPt(&(swedhome[4].hpoint), -7, 1); gridtopoint(&(swedhome[4].hpoint));
  51.     SetPt(&(swedhome[5].hpoint), -7, 2); gridtopoint(&(swedhome[5].hpoint));
  52.     SetPt(&(swedhome[6].hpoint), -7, 3); gridtopoint(&(swedhome[6].hpoint));
  53.     SetPt(&(swedhome[7].hpoint), -7, 4); gridtopoint(&(swedhome[7].hpoint));
  54.  
  55.     SetPt(&(muschome[0].hpoint), 6, -3); gridtopoint(&(muschome[0].hpoint));
  56.     SetPt(&(muschome[1].hpoint), 6, -2); gridtopoint(&(muschome[1].hpoint));
  57.     SetPt(&(muschome[2].hpoint), 6, -1); gridtopoint(&(muschome[2].hpoint));
  58.     SetPt(&(muschome[3].hpoint), 6, 0); gridtopoint(&(muschome[3].hpoint));
  59.     SetPt(&(muschome[4].hpoint), 6, 1); gridtopoint(&(muschome[4].hpoint));
  60.     SetPt(&(muschome[5].hpoint), 6, 2); gridtopoint(&(muschome[5].hpoint));
  61.     SetPt(&(muschome[6].hpoint), 6, 3); gridtopoint(&(muschome[6].hpoint));
  62.     SetPt(&(muschome[7].hpoint), 6, 4); gridtopoint(&(muschome[7].hpoint));
  63.     SetPt(&(muschome[8].hpoint), 7, -3); gridtopoint(&(muschome[8].hpoint));
  64.     SetPt(&(muschome[9].hpoint), 7, -2); gridtopoint(&(muschome[9].hpoint));
  65.     SetPt(&(muschome[10].hpoint), 7, -1); gridtopoint(&(muschome[10].hpoint));
  66.     SetPt(&(muschome[11].hpoint), 7, 0); gridtopoint(&(muschome[11].hpoint));
  67.     SetPt(&(muschome[12].hpoint), 7, 1); gridtopoint(&(muschome[12].hpoint));
  68.     SetPt(&(muschome[13].hpoint), 7, 2); gridtopoint(&(muschome[13].hpoint));
  69.     SetPt(&(muschome[14].hpoint), 7, 3); gridtopoint(&(muschome[14].hpoint));
  70.     SetPt(&(muschome[15].hpoint), 7, 4); gridtopoint(&(muschome[15].hpoint));
  71. }
  72.  
  73. /*
  74.  * makegrid() - initialize the record of what piece is on what square.
  75.  */
  76. makegrid()
  77. {
  78.     static struct pieceimage *realgrid[2+9+2][2+9+2]; /* the actual storage */
  79.     short h, v;
  80.  
  81.     gridp = &realgrid[2+4][2+4];    /* center the grid on [0][0]    */
  82.     for (h = -(2+4); h <= 4+2; ++h) {
  83.     for (v = -(2+4); v <= 4+2; ++v) {
  84.         (*gridp)[h][v] = (struct pieceimage *) 0;
  85.     }
  86.     }
  87. }
  88.  
  89. /*
  90.  * placepiece() - move the given piece to the given grid position
  91.  */
  92. placepiece(pidx, gh, gv)
  93. int pidx;        /* index of the piece to move        */
  94. short gh, gv;        /* grid coordinates to move the piece to */
  95. {
  96.     struct pieceimage *p;
  97.     Point destpoint;    /* window coordinates to move to    */
  98.  
  99.     p = &piece[pidx];
  100.     if (onscreen(p) && !onboard(p)) {
  101.     p->curhome->tenant = (struct pieceimage *) 0;
  102.     p->curhome = &weirdhome;
  103.     }
  104.     if (onboard(p)) {
  105.     (*gridp)[p->grid.h][p->grid.v] = (struct pieceimage *) 0;
  106.     }
  107.     p->grid.h = gh;  p->grid.v = gv;
  108.     (*gridp)[p->grid.h][p->grid.v] = p;
  109.     destpoint.h = gh; destpoint.v = gv;
  110.     gridtopoint(&destpoint);
  111.     sweeppiece(pidx, &destpoint);
  112. }
  113.  
  114. /*
  115.  * removepiece() - remove the given piece from the board,
  116.  *  placing it in the appropriate off-board position.
  117.  */
  118. removepiece(pidx)
  119. int pidx;        /* index of the piece to remove    */
  120. {
  121.     struct pieceimage *p;
  122.     int wasonboard;    /* true if the piece was originally on-board    */
  123.     struct piecehome *lasthp;    /* a temp home-pointer            */
  124.     struct piecehome *hp;    /* another temp                */
  125.  
  126.     p = &piece[pidx];
  127.     wasonboard = onboard(p);
  128.     if (wasonboard) {
  129.     (*gridp)[p->grid.h][p->grid.v] = (struct pieceimage *) 0;
  130.     }
  131.     p->grid.h = NOGRID; p->grid.v = NOGRID;
  132.     if (wasonboard || !onscreen(p)) {
  133.     switch(classbase(p)) {
  134.     case FIRSTSWEDE:
  135.         hp = &swedhome[0]; lasthp = &swedhome[LASTSWEDE - FIRSTSWEDE];
  136.         break;
  137.     case FIRSTMUSC:
  138.         hp = &muschome[0]; lasthp = &muschome[LASTMUSC - FIRSTMUSC];
  139.         break;
  140.     case THEKING:
  141.         hp = &kinghome[0]; lasthp = &kinghome[0];
  142.         break;
  143.     }
  144.     while (hp <= lasthp && hp->tenant) {
  145.         ++hp;
  146.     }
  147.     if (hp > lasthp) {
  148.         hp = &weirdhome;    /* should never happen */
  149.     }
  150.     hp->tenant = p;
  151.     p->curhome = hp;
  152.     }
  153.     sweeppiece(pidx, &(p->curhome->hpoint));
  154. }
  155.  
  156. /*
  157.  * hidepiece() - move a piece off the screen
  158.  */
  159. hidepiece(pidx)
  160. int pidx;    /* index of the piece to hide        */
  161. {
  162.     struct pieceimage *p;
  163.  
  164.     p = &piece[pidx];
  165.     if (onscreen(p) && !onboard(p)) {
  166.     p->curhome->tenant = (struct pieceimage *) 0;
  167.     p->curhome = &weirdhome;
  168.     }
  169.     if (onboard(p)) {
  170.     (*gridp)[p->grid.h][p->grid.v] = (struct pieceimage *) 0;
  171.     }
  172.     p->grid.h = NOGRID; p->grid.v = NOGRID;
  173.     drawpiece(pidx, NOPLACE, NOPLACE);
  174. }
  175.